/* TCSS 590A - Cybernetics and Systems
 * Group Project - Modeling
 * By Barry Jones, Naren Meadem, Michael Pitts, Yuan Tao
 * Fall 2012, University of Washington, Tacoma
 */
package model.delayed.strategic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import model.delayed.Event;


/**
 * A Strategic model controller
 * @author Barry Jones
 * @version Nov 30, 2012
 */
public class StratCon implements StrategicController
{

	private long Mincost = 0;
	private long Maxcost = -1000000000;
	private ArrayList<Long> CostStore = new ArrayList<Long>();
	private long Minwage = 1000000000;
	private long Maxwage = 0;
	private ArrayList<Long> WageStore = new ArrayList<Long>(); 
	
		// computes max and min cost and max and min wage/income, and adds all cost and
		//wage to correct Store list 
		@Override public Collection<Event> compute(long the_time, 
				final Collection<Event> the_events)
		{
		
		Event e; 
		Iterator<Event> itr = the_events.iterator();
			while (itr.hasNext())
			{
			e = itr.next();
			// checks if there is no account and if it's a cost
				if(e.account == null && e.value < 0)
				{
					CostStore.add(e.value);
					if (e.value < Mincost)
					{
					Mincost = e.value;
					}
					if (e.value > Maxcost)
					{
					Maxcost = e.value;
					}
				}
				// checks if there is no account and if it's a wage
				if(e.account == null && e.value > 0)
				{
					WageStore.add(e.value);
					if (e.value < Minwage)
					{
					Minwage = e.value;
					}
					if (e.value > Maxcost)
					{
					Maxwage = e.value;
					}
				}
			}
			return the_events;
		}
	
	// returns the smallest cost so far
	@Override public long getMinCost()
	{
		return Mincost;
	}
	// returns the average of all costs so far
	@Override public long getMeanCost()
	{
		long size = CostStore.size();
		if (size == 0) return 0;
		long total = 0;
		Iterator<Long> itr = CostStore.iterator();
		while (itr.hasNext())
			{
			total += itr.next();
			}
		return (total/size);
	}
	// returns  the largest cost so far
	@Override public long getMaxCost()
	{
		return Maxcost;
	}
	// returns the smallest wage so far
	@Override public long getMinWage()
	{
		return Minwage;
	}
	// returns the average of all wage so far
	@Override public long getMeanWage()
	{
		long size = WageStore.size();
		if (size == 0) return 0;
		long total = 0;

		Iterator<Long> itr = WageStore.iterator();
		while (itr.hasNext())
			{
			total += itr.next();
			}
		return (total/size);
	}
	// returns  the largest wage so far
	@Override public long getMaxWage()
	{
		return Maxwage;
	}
	
}